﻿using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Zocono.WCS.Domain.DomainRepository;
using Zocono.WCS.Domain.EntityInfo;
using Zocono.WMS.Infrastructure.BaseRepository;

namespace Zocono.WCS.Infrastructure.Repository
{
    public class PlcConnectionInfoRepository : BaseRepository<PlcConnectionInfo>, IPlcConnectionInfoRepository
    {
        public PlcConnectionInfoRepository(SqlSugarClient context = null) : base(context)
        {

        }

        public async Task<List<PlcConnectionInfo>> GetPlcConnectionInfosAsync(string programeCode)
        { 
            var exp = Expressionable.Create<PlcConnectionInfo, PlcReadDbInfo, JobInfo, ProgrameInfo>();
            exp.And((pc, pr, j, p) => p.ProgrameCode == programeCode);

            var result = await this.Context.Queryable<PlcConnectionInfo, PlcReadDbInfo, JobInfo, ProgrameInfo>(
                (pc, pr, j, p) => new JoinQueryInfos(
                    JoinType.Left, pc.ID == pr.PlcConnectionID,
                    JoinType.Left, pr.JobID == j.ID,
                    JoinType.Left, j.ProgrameCode == p.ProgrameCode))
                .Where(exp.ToExpression())
                .Select(pc => pc).Distinct().ToListAsync();
           
            return result;
        }
    }
}
